import Vue from 'vue'
import Router from 'vue-router'
import Login from './views/Login.vue'

Vue.use(Router)

const routers = [{
        path: '/',
        name: 'login',
        component: Login,
    },
    {
        path: '/about',
        name: 'about',
        meta: { requireToken: true },
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "about" */ './views/About.vue'),
    },
    {
        path: '/login',
        name: 'login',
        component: Login,
    },
    {
        path: '/register',
        name: 'register',
        component: () =>
            import ('./views/Register.vue'),
    },
    {
        path: '/index',
        name: 'index',
        meta: { requireToken: true },
        component: () =>
            import ('./views/Index.vue'),
    },
]

const router = new Router({ routes: routers })

//定义导航守卫
router.beforeEach((to, from, next) => {
    //获取token
    const token = localStorage.getItem('token')
        //判断Component组件名称
    if (to.meta.requireToken && to.name != 'login' && !token) {
        next({ name: 'login' })
    } else {
        next()
    }
})

export default router
// export default new Router({
//     ,
// })